<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\Business\Admin\Account\AccountManagement;
use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Order\OrderManagement;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends WebController
{
    //充值订单列表
    public function rechargeList() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $perpage = 50;
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $statisticResult = OrderManagement::getRechargeStatistical($startTime, $lastTime, $search);
        $result = OrderManagement::rechargeList($startTime, $lastTime, $search, $perpage);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'total' => $lists->total(),
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
            'perpage' => $perpage,
            'lists' => $lists,
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'search' => $search,
            'moneytotal' => ($statisticResult['status']=='success')?$statisticResult['data']['moneytotal'] : 0,
            'feetotal' => ($statisticResult['status']=='success')?$statisticResult['data']['feetotal'] : 0,
            'actualmoneytotal' => ($statisticResult['status']=='success')?$statisticResult['data']['actualmoneytotal'] : 0,
        ];
        return view('console.order.recharge_list', $data);
    }
    //导出充值列表
    public function exportRechargeList() {
        $startTime = $this->request->get('starttime');
        $startTime = $startTime . " 00:00:00";
        $lastTime = $this->request->get('lasttime');
        $lastTime = $lastTime . " 23:59:59";
        $result = OrderManagement::exportRechargeList($startTime, $lastTime);
        if($result['status'] == 'failed') {
            return $this->responseError('导出失败');
        }
        $data = $result['data'];
        $cellData = [
            ['充值id', '手机号','昵称','充值号','充值途径','金额','服务费','实付金额','翻倍状态','时间']
        ];
        foreach($data as $list) {
            $newList = [
                'rechargeid' => $list['rechargeid'],
                'mobile' => $list['mobile'],
                'nickname' => $list['nickname'],
                'rechargenumber' => $list['rechargenumber'],
                'path' => $list['path'],
                'money' => $list['money'],
                'fee' => $list['fee'],
                'actualmoney' => $list['actualmoney'],
                'doublestatus' => $list['doublestatus'],
                'created_at' => date('Y-m-d H:i:s', strtotime($list['created_at']))
            ];
            array_push($cellData, $newList);
        }
        $title = '充值列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('充值列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //修改充值翻倍状态
    public function updateRechargeDoubleStatus() {
        $rechargeNumber = $this->request->get('rechargenumber');
        $userId = $this->request->get('userid');
        $cityId = $this->request->get('cityid');
        $result = OrderManagement::updateRechargeDoubleStatus($userId, $rechargeNumber);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return redirect()->to("/console/order/recharge/list?cityid=".$cityId);
        }
    }
    //充值记录详情-前端
    public function getRechargeDetail() {
        $rechargeId = $this->request->get('rechargeid');
        $result = OrderManagement::rechargeDetails($rechargeId);
        if($result['status'] == 'failed') {
            return view("errors.404");
        }
        $data = $result['data'];
        return view('console.order.recharge_detailsWEB', $data);
    }
    //消费订单列表
    public function consumeList() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $perpage = 50;
        $totalResult = OrderManagement::getOrderStatistical($cityId, $startTime, $lastTime, $search);
        $result = OrderManagement::orderList($cityId, $startTime, $lastTime, $search, $perpage);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $data = [
            'roleid' => $roleId,
            'total' => ($totalResult['status'] == 'success') ? $totalResult['data']['total'] : 0,
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
            'perpage' => $perpage,
            'lists'=>$lists,
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'search' => $search,
            'usersweepcount' => ($totalResult['status'] == 'success') ? $totalResult['data']['usersweepcount'] : 0,
            'shopsweepcount' => ($totalResult['status'] == 'success') ? $totalResult['data']['shopsweepcount'] : 0,
            'sum' => ($totalResult['status'] == 'success') ? $totalResult['data']['sum'] : 0,
            'redenvelopeamount' => ($totalResult['status'] == 'success') ? $totalResult['data']['redenvelopeamount'] : 0,
            'actualamount' => ($totalResult['status'] == 'success') ? $totalResult['data']['actualamount'] : 0,
            'redenvelopebonus' => ($totalResult['status'] == 'success') ? $totalResult['data']['redenvelopebonus'] : 0,
        ];
        return view('console.order.consume_list', $data);
    }
    //导出消费订单列表
    public function exportConsumeList() {
        $cityId = $this->request->get('cityid');
        $startTime = $this->request->get('starttime');
        $startTime = $startTime . " 00:00:00";
        $lastTime = $this->request->get('lasttime');
        $lastTime = $lastTime . " 23:59:59";
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $result = OrderManagement::exportOrderList($cityId, $startTime, $lastTime, $search);
        if($result['status'] == 'failed') {
            return $this->responseError('导出失败');
        }
        $cellData = [
            ['订单id', '订单号', '商家编码','用户手机','用户昵称','商家名称','一级分类','二级分类','订单金额','实付金额','红包金额','打款金额','创建时间','商务人员','旧商务人员']
        ];
        $data = $result['data'];
        foreach($data as $list) {
            $orderNumber = ($list['refundstatus']==1)? "退".$list['ordernumber'] : $list['ordernumber'];
            $newList = [
                'orderid' => $list['orderid'],
                'ordernumber' => $orderNumber,
                'code' => $list['code'],
                'mobile' => $list['mobile'],
                'nickname' => $list['nickname'],
                'shopname' => $list['shopname'],
                'firstclassname' => $list['firstclassname'],
                'secondclassname' => $list['secondclassname'],
                'orderamount' => $list['orderamount'],
                'actualamount' => $list['actualamount'],
                'redenvelopeamount' => $list['redenvelopeamount'],
                'accountamount' => $list['accountamount'],
                'created_at' => date('Y-m-d H:i:s',strtotime($list['created_at'])),
                'adminname' => $list['adminname'],
                'oldmaintainer' => $list['oldmaintainer']
            ];
            array_push($cellData, $newList);
        }
        $title = '消费订单列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('消费订单列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //消费订单列表
    public function consumeDetails() {
        $orderId = $this->request->get('orderid');
        $result = OrderManagement::orderDetails($orderId);
        $cityId = $this->request->get('cityid');
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        return view('console.order.consume_details', $data);
    }
    //当订单退款获取退款金额
    public function getMoneyAfterOrderRefund() {
        $cityId = $this->request->get('cityid');
        $userId = $this->request->get('userid');
        $orderNumber = $this->request->get('ordernumber');
        $result = OrderManagement::getMoneyAfterOrderRefund($userId, $orderNumber);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //提交订单退款数据
    public function submitOrderRefundData() {
        $cityId = $this->request->get('cityid');
        $userId = $this->request->get('userid');
        $orderNumber = $this->request->get('ordernumber');
        $refundRemark = $this->request->get('refundremark');
        $password = $this->request->get('password');
        if(env('WEB_URL') == 'https://www.fbw-china.com/') {
            if($password != 'fbw1984') {
                return $this->responseError('密码错误');
            }
        } else {
            if($password != 'fbw123') {
                return $this->responseError('密码错误');
            }
        }
        $result = AccountManagement::orderRefund($orderNumber, $refundRemark);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage('退款成功');
    }
    //所有评价列表
    public function commentList() {
        $cityId = $this->request->get("cityid");
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $perpage = 50;
        $result = OrderManagement::commentList($cityId, $startTime, $lastTime, $search, $perpage);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'total' => $lists->total(),
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
            'perpage' => $perpage,
            'lists' => $lists,
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'search' => $search,
        ];
        return view('console.order.comment_list', $data);
    }
    //删除评论
    public function deleteComment() {
        $cityId = $this->request->get('cityid');
        $commentId = $this->request->get('commentid');
        $result = OrderManagement::delComment($commentId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //举报评价列表
    public function reportList() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 50;
        $skip = ($page-1)*$length;
        $result = OrderManagement::reportList($startTime, $lastTime, $search, $skip, $length);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['lastpage'] = ceil($data['total'] / $length);
        $data['page'] = $page;
        $data['perpage'] = $length;
        $data['cityid'] = $cityId;

        return view('console.order.report_list', $data);
    }
    //处理举报状态
    public function handleReportStatus() {
        $type = $this->request->get('type');
        $commentId = $this->request->get('commentid');
        $result = OrderManagement::handleReportStatus($type, $commentId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //商家回评列表
    public function businessReview() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 50;
        $skip = ($page - 1) * $length;
        $result = OrderManagement::shopCommentList($cityId, $startTime, $lastTime, $search, $skip, $length);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];

        return view('console.order.business_review', $data);
    }
    //删除商家回评
    public function deleteBusinessReview() {
        $commentId = $this->request->get('commentid');
        $result = OrderManagement::delShopComment($commentId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //用户充值退款记录
    public function rechargeRefundList() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $perpage = 50;
        $result = OrderManagement::rechargeRefundList($startTime, $lastTime, $search, $perpage);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $totalResult = OrderManagement::getRechargeRefundStatistic($startTime, $lastTime, $search);
        if($totalResult['status'] == 'failed') {
            $moneyTotal = 0.00;
            $feeTotal = 0.00;
            $actualMoneyTotal = 0.00;
            $count = 0;
        } else {
            $moneyTotal = $totalResult['data']['moneytotal'];
            $feeTotal = $totalResult['data']['feetotal'];
            $actualMoneyTotal = $totalResult['data']['actualmoneytotal'];
            $count = $totalResult['data']['count'];
        }
        $data = [
            'total' => $lists->total(),
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
            'perpage' => $perpage,
            'lists' => $lists,
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'search' => $search,
            'moneytotal' => $moneyTotal,
            'feetotal' => $feeTotal,
            'actualmoneytotal' => $actualMoneyTotal
        ];
        return view('console.order.refund_list', $data);
    }
    //导出充值退款记录
    public function rechargeRefundListExport(){
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $result = OrderManagement::rechargeRefundListExport($cityId,$startTime, $lastTime, $search);
        if($result['status'] == 'failed'){
            return $this->responseError('导出失败');
        }
        $cellData = [['退款时间','用户昵称','用户账号','退款金额(元)','服务费(元)','退款实际金额(元)','退款前余额','退款后余额','备注']];
        foreach($result['data'] as $list) {
            $newList = [
                'created_at' => date("Y-m-d H:i",strtotime($list['created_at'])),
                'nickname' => $list['nickname'],
                'mobile' => $list['mobile'],
                'nickname' => $list['nickname'],
                'money' => $list['money'],
                'fee'  =>$list['fee'],
                'actualmoney'  =>$list['actualmoney'],
                'startbalance'  =>$list['startbalance'],
                'lastbalance'  =>$list['lastbalance'],
                'note'  =>$list['note'],
            ];
            array_push($cellData, $newList);
        }
        $title = '充值退款记录列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('充值退款记录列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //用户退款记录
    public function orderRefundList() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $statisticArray = OrderManagement::getOrderRefundStatistic($cityId, $startTime, $lastTime, $search);
        $total =  $statisticArray['orderrefundcount'];
        $sum = $statisticArray['orderrefundsum'];
        $result = OrderManagement::orderRefundList($cityId, $startTime, $lastTime, $search);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $perPage = 50;
        $data = [
            'total' => $total,
            'lastpage' => ceil($total/$perPage),
            'page' => $page,
            'perpage' => $perPage,
            'lists' => $lists,
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'search' => $search,
            'orderrefundcount' => $total,
            'orderrefundsum' => $sum,

        ];
        return view('console.order.order_refund_list', $data);
    }

    //导出订单消费记录
    public function orderRefundListExport(){
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $result = OrderManagement::orderRefundListExport($cityId, $startTime, $lastTime, $search);
        if($result['status'] == 'failed'){
            return $this->responseError('导出失败');
        }
        $cellData = [['退款时间','消费单号','用户账号','用户昵称','退款金额(元)','红包变更', '备注']];
        foreach($result['data'] as $list) {
            $newList = [
                'created_at' => $list['created_at'],
                'ordernumber' => $list['ordernumber'],
                'mobile' => $list['mobile'],
                'nickname' => $list['nickname'],
                'money' => $list['money'],
                'redenvelopeinfo'  =>$list['redenvelopeinfo'],
                'remark' => $list['remark']
            ];
            array_push($cellData, $newList);
        }
        $title = '消费退款记录列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('消费退款记录列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
}